import { message } from "antd";
import { trim } from "../../Tools";
import { periodTypeData } from "../../api/DesignTableApi";
export default class TableEditorService {
    context;
    constructor(context) {
        this.context = context;
        this.handleColorChange = this.handleColorChange.bind(this);
        this.handleBgColorChange = this.handleBgColorChange.bind(this);
    }
    zixingChange(e) {
        const currentZiXing = e.itemData.id;
        //设置字形
        this.setCellFontStyle(currentZiXing);
        this.context.setState({
            it: currentZiXing,
        });
    }
    setCellFontStyle(currentZiXing) {
        const currentRange = this.context.gridRef.state.currentRange;
        if (!currentRange) {
            this.context.showErrorNotify("请选择单元格进行操作");
            return;
        }
        this.context.gridService.setCellFontStyle(currentZiXing, currentRange);
        this.context.gridRef.setTableContentChange(true, {
            type: "设置单元格字形",
            params: { it: currentZiXing },
        });
    }
    zihaoChange(e) {
        const currentZiHao = e.itemData.id;
        this.context.setState({
            currentZiHao,
        });
    }
    zitiColorChange(e) {
        const currentZiTiColor = e.itemData.id;
        this.context.setState({
            currentZiTiColor,
        });
    }
    handleColorChange({ value }) {
        this.context.setState({
            fc: value,
        });
    }
    setColorToCell() {
        if (!this.context.gridRef.state.currentRange) {
            this.context.showErrorNotify("请选择单元格进行操作");
            return;
        }
        const currentRange = this.context.gridRef.state.currentRange;
        // this.gridService.setRetractToCell(value, currentRange);
        this.context.gridService.setCellConfig({ fc: this.context.state.fc }, currentRange);
        this.context.gridRef.setTableContentChange(true, {
            type: "设置单元格字体颜色",
            params: { fc: this.context.state.fc },
        });
    }
    handleBgColorChange({ value }) {
        this.context.setState({
            bg: value,
        });
    }
    setBgColorToCell() {
        if (!this.context.gridRef.state.currentRange) {
            this.context.showErrorNotify("请选择单元格进行操作");
            return;
        }
        const currentRange = this.context.gridRef.state.currentRange;
        // this.gridService.setRetractToCell(value, currentRange);
        this.context.gridService.setCellConfig({ bg: this.context.state.bg }, currentRange);
        this.context.gridRef.setTableContentChange(true, {
            type: "设置单元格背景颜色",
            params: { bg: this.context.state.bg },
        });
    }
    /**
     * 设置单元格对齐方式
     */
    setCellAlign(type) {
        if (!this.context.gridRef.state.currentRange) {
            this.context.showErrorNotify("请选择单元格进行操作");
            return;
        }
        let handleType = type;
        this.context.gridService.setCellConfig({ ht: handleType }, this.context.gridRef.state.currentRange);
        if (this.context.state.ht === type) {
            handleType = 0;
        }
        this.context.setState({
            ht: handleType,
        });
        this.context.gridRef.setTableContentChange(true, {
            type: "设置单元格对齐方式",
            params: { ht: handleType },
        });
    }
    /**
     * 设置表头对齐方式
     * type : 1 左对齐 ， 2居中对齐 ， 右对齐
     */
    setHeaderAlign(type) {
        //TODO 批量设置，而不是for循环 是范围划选，暂时官方没有提供该Api.目前采用dom形式进行抓取
        const rangeKeys = this.context.gridRef.headerService.getNodeKeyBySelected();
        if (rangeKeys.length < 1) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        let ht = 2;
        if (this.context.state.headerHt === type) {
            // 重置表头
            rangeKeys.forEach((key) => {
                this.context.gridRef.headerService.updateNodeByKeyAndRefresh({ ht: ht }, key, true);
            });
            this.context.setState({
                headerHt: 0,
            });
        }
        else {
            ht = type;
            rangeKeys.forEach((key) => {
                this.context.gridRef.headerService.updateNodeByKeyAndRefresh({ ht: ht }, key, true);
            });
            this.context.setState({
                headerHt: ht,
            });
        }
        this.context.gridRef.setTableContentChange(true, {
            type: "设置表头对齐方式",
            params: { ht: ht },
        });
    }
    setHeaderBold(type) {
        const rangeKeys = this.context.gridRef.headerService.getNodeKeyBySelected();
        console.log(rangeKeys, "rangeKeys----", this.context.gridRef);
        if (rangeKeys.length < 1) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        const headerBl = this.context.state.headerBl;
        let val = 1;
        if (headerBl === type) {
            val = 2;
        }
        else {
            val = type;
        }
        this.context.setState({
            headerBl: val,
        });
        // ht = type;
        rangeKeys.forEach((key) => {
            this.context.gridRef.headerService.updateNodeByKeyAndRefresh({ bl: val }, key, true, true);
        });
        this.context.gridRef.setTableContentChange(true, {
            type: "设置表头是否加粗",
            params: { bl: val },
        });
    }
    /**
     * 设置加粗
     */
    setCellFontBold() {
        const currentRange = this.context.gridRef.state.currentRange;
        if (!currentRange) {
            this.context.showErrorNotify("请选择单元格进行操作");
            return;
        }
        const val = this.context.state.bl === 1 ? 2 : 1;
        this.context.gridService.setCellFontBold(val, currentRange);
        this.context.setState({
            ...this.context.state,
            bl: val,
        });
        this.context.gridRef.setTableContentChange(true, {
            type: "设置字体加粗",
            params: { bl: val },
        });
    }
    /**
     * 设置逆序
     */
    async setReverseOrder() {
        const currentRecord = this.context.gridRef.state.currentRecord;
        if (!currentRecord) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        if (!currentRecord.key) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        const ro = this.context.state.ro;
        let val = ro;
        if (ro === undefined) {
            val = true;
        }
        else if (ro === true) {
            val = false;
        }
        else if (ro === false) {
            val = undefined;
        }
        // 二. 先添加列
        const key = this.context.gridRef.state.currentRecord.key;
        let currentGridData = this.context.gridRef.getCpasTableGrid();
        currentGridData.tableConfig["isNx"] = val;
        // dbname, action,type, key, iGridData
        console.log(currentGridData, "currentGridData----");
        const result = await periodTypeData({
            action: "reCalculate",
            type: "",
            key,
            iGridData: currentGridData,
        });
        if (result.ok) {
            await this.context.gridRef.reloadTable(result.data, null, null, true);
        }
        else {
            this.context.showErrorNotify("设置逆序失败");
        }
        this.context.setState({
            ...this.context.state,
            ro: val,
        });
        this.context.gridRef.setTableContentChange(true, {
            type: "设置单元格逆序",
            params: { ro: val },
        });
    }
    /**
     * 添加行的时候，会复制当前行的属性。如标签，权重等信息。并进行聚合行的公式刷新
     * 插入行
     */
    insertRowsHandle(e, isCopy, insertCount) {
        if (this.context.gridRef.data.length > 501) {
            this.context.showErrorNotify("最多只能500行");
            return;
        }
        const { id } = e.itemData;
        let type = "";
        if (id === 2) {
            type = "top";
        }
        else {
            type = "bottom";
        }
        const currentRecord = this.context.gridRef.state.currentRecord;
        let insertIndex = this.context.hot.getData().length; //默认没有选择在尾部添加
        let row = this.context.gridRef.state.currentRecord.coords.row;
        if (currentRecord) {
            if (row < 0) {
                row = 0;
            }
            insertIndex = type === "top" ? row : row + 1;
        }
        if (insertIndex === 0 && type === "top") {
            message.info("不能在隐藏行上面插入行");
            return;
        }
        //批量下方添加
        if (id === 3) {
            this.context.batchInsertPopup.current.show({
                title: "批量插行",
                okCallback: (v) => {
                    if (!v || trim(v) === "") {
                        message.info("请输入要插入的行数");
                        return;
                    }
                    if (isNaN(v)) {
                        message.info("输入的行数必须为数字");
                        return;
                    }
                    if (v <= 0) {
                        message.info("输入的行数至少为1");
                        return;
                    }
                    if (insertIndex === 1 && type === "bottom") {
                        isCopy = false; // 解决在隐藏行下面插行无法显示单元格背景等央视问题 bug-452
                    }
                    this.context.gridService.addRow(type, isCopy, insertIndex, this.context.state.currentGroupKeyColumn, parseInt(v));
                },
            });
        }
        else {
            if (insertIndex === 1 && type === "bottom") {
                isCopy = false; // 解决在隐藏行下面插行无法显示单元格背景等央视问题 bug-452
            }
            this.context.gridService.addRow(type, isCopy, insertIndex, this.context.state.currentGroupKeyColumn);
        }
        if (currentRecord) {
            const after_handle_coords = this.context.hot.getSelectedRangeLast().from;
            this.context.gridRef.resetState({
                currentRecord: {
                    coords: after_handle_coords,
                    key: null,
                },
            });
        }
        if (id === 3) {
            this.context.gridRef.hot.deselectCell();
        }
        this.context.gridRef.setTableContentChange(true, {
            type: "新增行",
            params: { type, isCopy, insertIndex },
        });
    }
}
